<?php
require 'config.php';
require CAMINHO_SERVIDOR . 'funcoes.php';
require 'login.class.php';
$login = new Login();

if ($login->verificar(URL . 'index.php')) {
    if (empty($_POST["id_usuario"])) {
        $_POST["id_usuario"] = $_GET["id_usuario"];
    }
    $query = "SELECT (SELECT placar_time_A FROM tb_apostas WHERE id_jogo=j.id_jogo AND id_usuario=" . $_POST["id_usuario"] . ") AS aposta_a,
                (SELECT placar_time_B FROM tb_apostas WHERE id_jogo=j.id_jogo AND id_usuario=" . $_POST["id_usuario"] . ") AS aposta_b,
                j.local AS LOCAL,DATE_FORMAT(j.data,'%d/%m/%Y') AS `data`,j.hora AS hora,cam.descricao AS campeonato,
                ta.img AS img_a,tb.img AS img_b,ta.nome AS time_a,tb.nome AS time_b,j.`id_jogo`, j.`id_time_A`,
                j.`id_time_B`, j.`descricao`,j.`placar_time_A`, j.`placar_time_B`, j.`id_campeonato`, j.`status`, 
                j.`local`, j.`obs_placar`, j.`diferenca_gols`,j.`id_rodada`, j.`data_cadastro`
                FROM tb_jogos j
                INNER JOIN tb_time ta ON ta.id_time=j.id_time_A
                INNER JOIN tb_time tb ON tb.id_time=j.id_time_B 
                INNER JOIN tb_campeonato cam ON cam.id_campeonato=j.id_campeonato";
    $jogos = $login->executaQueryAll($query);
    $query = "SELECT * FROM tb_usuario WHERE id_usuario=" . $_POST['id_usuario'];
    $usuario = $login->executaQueryRow($query);
}
?>
<html>
    <head>
        <title>Jogo</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width">
        <link rel="stylesheet" type="text/css" href="bootstrap/css/bootstrap.css">
        <link rel="stylesheet" type="text/css" href="bootstrap/css/classes.css">
    </head>
    <body>
        <div class="container">
            <div class="container-fluid margin10px">
                <a class="btn btn-info" href="<?php echo URL_ADMIN; ?>" >Início</a>
            </div>
            <div class="container-fluid">
                <div class="col-md-12 col-md-offset-0">
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                            <h3 class="panel-title text-center"><?php echo $jogos[0]["campeonato"]; ?> - Apostas de <?php echo $usuario["nome"]; ?></h3>
                        </div>
                        <div class="panel-body text-center">
                            <table class="table">
                                <thead>
                                    <tr class="info">
                                        <th colspan="3" class="text-center">Jogo</th>
                                        <th class="text-center">Data</th>
                                        <th class="text-center">Horário</th>
                                        <th colspan="2" class="text-center">Minhas apostas</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php
                                    foreach ($jogos as $i => $jogo) {
                                        if ($jogo["aposta_a"] == "" && $jogo["aposta_b"] == "") {
                                            $cor = "danger";
                                        } else {
                                            $cor = "active";
                                        }
                                        ?>
                                        <tr class="<?php echo $cor; ?>">
                                            <td class="text-right"><?php echo$jogo['time_a']; ?></td>
                                            <td class="text-center" style="width: 120px">
                                                <img src="imagens/<?php echo $jogo["img_a"]; ?>" alt="<?php echo $jogo["time_a"]; ?>" height="30" width="30">
                                                &nbsp;
                                                x
                                                &nbsp;
                                                <img src="imagens/<?php echo $jogo["img_b"]; ?>" alt="<?php echo $jogo["time_b"]; ?>" height="30" width="30">
                                            </td>
                                            <td class=""><?php echo$jogo['time_b']; ?></td>
                                            <td class="text-center" style="width: 120px"><?php echo$jogo['data']; ?></td>
                                            <td class="text-center" style="width: 120px"><?php echo$jogo['hora']; ?></td>
                                            <td class="text-center" >
                                                <a class='btn btn-info' href="<?php echo URL ?>apostar.php?id_jogo=<?php echo$jogo['id_jogo']; ?>&id_usuario=<?php echo $_POST['id_usuario']; ?>">Apostar</a>
                                            </td>
                                            <td>
                                                <p style='color:#6d7275;text-align:center;' class='negrito'>
                                                    <img src="imagens/<?php echo $jogo["img_a"]; ?>" alt="<?php echo $jogo["time_a"]; ?>" height="30" width="30">
                                                    <?php
                                                    echo($jogo["aposta_a"] == "" ? " _ " : $jogo["aposta_a"]);
                                                    echo " x ";
                                                    echo($jogo["aposta_b"] == "" ? " _ " : $jogo["aposta_b"]);
                                                    ?> 
                                                    <img src="imagens/<?php echo $jogo["img_b"]; ?>" alt="<?php echo $jogo["time_b"]; ?>" height="30" width="30">
                                                </p>
                                            </td>
                                        </tr>
                                        <?php
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>